﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlayerStat
{
    public class EventInit
    {
        private DateTime start_date = DateTime.Parse("2005-08-06");
        private int start_year = 2005;
        private DateTime dt;
        private int event_year;
        private int nRound;
        private int max_round = 0;
        private bool bFistTime;

        public EventInit(int leagueID)
        {
            DBUtil db = new DBUtil();

            if (!db.FindMaxDate(leagueID, ref dt))
            {
                dt = start_date;
            }
            else
            {
                //The DB has the data, remove the data in last date. 
                //The loop will start from the last date
                db.ClearDataByDay(leagueID, dt);
            }
            event_year = dt.Month >= 7 ? dt.Year : dt.Year - 1;
            bFistTime = true;
        }

        public EventInit(int leagueID, string countryID)
        {
            DBUtil db = new DBUtil();
            if (countryID == Leagues.ScotlandID.ToString())
            {
                if (!db.FindMinDate(leagueID, ref dt))
                {
                    dt = start_date.AddYears(4);//Scotland date is from 2009
                }
                event_year = dt.Month >= 7 ? dt.Year : dt.Year - 1;
                if (event_year < 2009)
                {
                    event_year = 2009;
                }
                if (dt == start_date.AddYears(4))
                {
                    dt = dt.AddYears(1);
                }
            }
            else
            {
                if (!db.FindMaxDate(leagueID, ref dt))
                {
                    dt = start_date;
                }
                event_year = dt.Month >= 7 ? dt.Year : dt.Year - 1;
                if (countryID == Leagues.FranceID.ToString() && event_year < start_year)
                {
                    event_year = start_year;
                }
            }
            nRound = 0;
            bFistTime = true;
        }

        public EventInit(int leagueID, int maxRound, string countryID)
        {
            DBUtil db = new DBUtil();

            max_round = maxRound;
            bFistTime = true;

            if (!db.FindMaxRound(leagueID, ref dt, ref nRound))
            {
                if (countryID == Leagues.FranceID.ToString())
                {
                    dt = start_date.AddYears(3);//start as 2008-8-6 
                    start_year = 2008;
                }
                else if (countryID == Leagues.NorwayID.ToString())
                {
                    dt = start_date.AddYears(4);//start as 2009-8-6 
                    start_year = 2006;
                }
                else if (countryID == Leagues.SwedenID.ToString())
                {
                    dt = start_date.AddYears(4);//start as 2009-8-6 
                    start_year = 2006;
                }
                else if (countryID == Leagues.AustriaID.ToString())
                {
                    dt = DateTime.Parse("2005-07-12");
                    start_year = 2005;
                }
                else
                {
                    dt = start_date;
                }

                nRound = 1;
                event_year = start_year;
            }
            else
            {
                if (dt.Month < 7)
                {
                    event_year = dt.Year - 1;
                }
                else
                {
                    event_year = dt.Year;
                }
                //The DB has the data, remove the data in last date. 
                //The loop will start from the last date
                //db.ClearDataByDay(leagueID, dt);
            }
        }

        public bool GetEventDate(int leagueID, out string date)
        {
            if (dt > DateTime.Today)
            {
                date = string.Empty;
                return false;
            }
            else
            {
                date = dt.ToString("yyyyMMdd");
                dt = dt.AddDays(1);
                return true;
            }
        }

        public bool GetEventYear(int leagueID, out string date, out int year)
        {
            int cur_year = DateTime.Now.Month >= 7 ? DateTime.Now.Year : DateTime.Now.Year - 1;
            if (event_year > cur_year)
            {
                date = string.Empty;
                year = 2009;
                return false;
            }
            else
            {
                if (bFistTime)
                {
                    date = dt.ToString("yyyy-MM-dd");
                    bFistTime = false;
                }
                else
                {
                    date = string.Empty;
                }
                year = event_year;
                event_year = event_year + 1;
                return true;
            }
        }

        public bool GetEventRound(int leagueID, out int year, out int round, out string date)
        {
            if (nRound > max_round)
            {
                int cur_year = DateTime.Now.Month >= 7 ? DateTime.Now.Year : DateTime.Now.Year - 1;

                if (event_year + 1 > cur_year)
                {
                    round = 0;
                    date = string.Empty;
                    year = 2007;
                    return false;
                }
                else
                {
                    NewSeason();
                    event_year = event_year + 1;
                    round = nRound;
                    year = event_year;
                    date = string.Empty;
                    return true;
                }
            }
            else
            {
                year = event_year;
                round = nRound;
                if (bFistTime)
                {
                    date = dt.ToString("yyyy-MM-dd");
                    bFistTime = false;
                }
                else
                {
                    date = string.Empty;
                }
                return true;
            }
        }

        // new season, it should reset the round
        public void NewSeason()
        {
            nRound = 1;
        }

        public void NextRound()
        {
            nRound = nRound + 1;
        }
    }
}
